package pers.xx.sbm.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.xx.sbm.entity.SystemAdmin;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.service.SystemService;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;

import java.util.Date;

/**
 * @author ：xiexing
 * @description：配置首页
 * @date ：2020/8/11 10:40
 */
@Controller
@RequestMapping("/systemAdmin")
public class SystemAdminController {
    @Autowired
    private SystemService systemService;

    @Autowired
    private UserUtils userUtils;

    /**
     * @description 系统管理
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/")
    public String systemPage() {
        return "page/system/system";
    }

    /**
     * @description 获取系统列表
     * @author XieXing
     * @date 2020/8/16 12:02
     */
    @RequestMapping("/getSystemList")
    @ResponseBody
    public ResResult<PageInfo> getSystemList(SystemAdmin system, Integer page, Integer limit) {
        ResResult resResult = systemService.listByPage(system, page, limit);
        resResult.setOptName("获取系统列表");
        return resResult;
    }

    /**
     * @description 保存系统信息
     * @author XieXing
     * @date 2020/8/23 21:35
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult saveSys(SystemAdmin system) {
        User user = userUtils.getCurrentUser();
        system.setSysAddby(user.getUserName());
        system.setSysAddbyId(user.getId());
        system.setSysAddTime(new Date());
        ResResult resResult = systemService.insert(system);
        resResult.setOptName("保存系统信息");
        return resResult;
    }


    /**
     * @description 更新系统信息
     * @author XieXing
     * @date 2020/8/23 22:01
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResResult updateSys(SystemAdmin system) {
        User user = userUtils.getCurrentUser();
        system.setSysUpdateby(user.getUserName());
        system.setSysUpdatebyId(user.getId());
        system.setSysUpdateTime(new Date());
        ResResult resResult = systemService.updateByPrimaryKeySelective(system);//更新系统信息
        resResult.setOptName("更新系统信息");
        return resResult;
    }

    /**
     * @description 删除系统信息
     * @author XieXing
     * @date 2020/8/23 22:01
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public ResResult delMenu(@PathVariable(value = "id") Integer id) {
        ResResult resResult = systemService.deleteByPrimaryKey(id);//
        resResult.setOptName("删除系统信息");
        return resResult;
    }


    /**
     * @description 更新系统状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */
    @RequestMapping("/{id}/{status}")
    @ResponseBody
    public ResResult updateDataStatus(@PathVariable(value = "id") Integer id, @PathVariable(value = "status") Integer status) {
        //TODO 从session获取当前操作用户信息
        ResResult resResult = systemService.updateDataStatus(id, status);
        resResult.setOptName("更新系统状态");
        return resResult;
    }

    /**
     * @description 更新系统状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */

    /**
      *@description 获取系统id和名称
      *@author XieXing
      *@date 2020/11/18 18:06
      */
    @RequestMapping(value = "/getSystems",method = RequestMethod.POST)
    @ResponseBody
    public ResResult getSystems() {
        //TODO 从session获取当前操作用户信息
        ResResult resResult = systemService.selectSelective(null);
        resResult.setOptName("获取系统id和名称");
        return resResult;
    }

}
